package com.shopping.shoppingdemo.domain.po;

import com.shopping.shoppingdemo.domain.dto.AuditResult;
import com.shopping.shoppingdemo.enums.AfterSaleStatus;
import com.shopping.shoppingdemo.enums.AuditAction;

import java.time.Instant;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author Yandel
 * @date 2025/10/8 下午8:24
 * @description:
 */

public class AfterSaleOrder {
    private final String id;
    private String productName;
    private String purchaseStore;
    private long purchaseAmountCents;
    private Instant purchaseTime;
    private String problemDescription;
    private String contactPhone;

    private final AtomicReference<AfterSaleStatus> status = new AtomicReference<>(AfterSaleStatus.PENDING);
    private final List<AuditRecord> auditHistory = new CopyOnWriteArrayList<>();
    private final long createdAt = Instant.now().toEpochMilli();
    private volatile long version = 0;


    public AfterSaleOrder(String id) {
        this.id = id;
    }


    public synchronized AuditResult audit(String operator, AuditAction action, String comment) {
        AfterSaleStatus current = status.get();
        if (current != AfterSaleStatus.PENDING) {
            return new AuditResult(false, "重复或无效操作：当前状态为 " + current);
        }
        AfterSaleStatus newStatus = (action == AuditAction.APPROVE) ? AfterSaleStatus.APPROVED : AfterSaleStatus.REJECTED;
        status.set(newStatus);
        auditHistory.add(new AuditRecord(operator, action, comment, Instant.now()));
        version++;
        return new AuditResult(true, "操作成功，状态已改为 " + newStatus);
    }

    public String getId() {
        return id;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getPurchaseStore() {
        return purchaseStore;
    }

    public void setPurchaseStore(String purchaseStore) {
        this.purchaseStore = purchaseStore;
    }

    public long getPurchaseAmountCents() {
        return purchaseAmountCents;
    }

    public void setPurchaseAmountCents(long purchaseAmountCents) {
        this.purchaseAmountCents = purchaseAmountCents;
    }

    public Instant getPurchaseTime() {
        return purchaseTime;
    }

    public void setPurchaseTime(Instant purchaseTime) {
        this.purchaseTime = purchaseTime;
    }

    public String getProblemDescription() {
        return problemDescription;
    }

    public void setProblemDescription(String problemDescription) {
        this.problemDescription = problemDescription;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    public AtomicReference<AfterSaleStatus> getStatus() {
        return status;
    }

    public List<AuditRecord> getAuditHistory() {
        return auditHistory;
    }

    public long getCreatedAt() {
        return createdAt;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }
}
